package com.ysd.lis.controller.inv;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.BaseEntity;
import com.ysd.lis.entity.inv.InvStoreManage;
import com.ysd.lis.entity.inv.InvUserManage;
import com.ysd.lis.service.inv.InvUserManageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户仓库 前端控制器
 * </p>
 *
 * @author WH
 * @since 2024-09-11
 */
@RestController
@RequestMapping("/inv/userManage")
public class InvUserManageController extends BaseController {

    @Autowired
    private InvUserManageService invUserManageService;

    @ApiOperation("根据仓库获取用户")
    @GetMapping("/findUserManageList")
    public Result findUserManageList(InvUserManage invUserManage) {

        return invUserManageService.findUserManageList(invUserManage);
    }

    @ApiOperation("批量新增或修改")
    @PostMapping("/saveOrUpdateUserManage")
    public Result saveOrUpdateUserManage(@RequestBody List<InvUserManage> invUserManages) {
        Set<String> nameSet = new HashSet<>();
        List<InvUserManage> userManageList = new ArrayList<>();
        for (InvUserManage userManage : invUserManages) {
            if (!nameSet.contains(userManage.getUserCode()) && StringUtils.isNotBlank(userManage.getUserCode())) {
                nameSet.add(userManage.getUserCode());
                userManageList.add(userManage);
            }
        }

        boolean b = invUserManageService.saveOrUpdateBatch(userManageList);
        if (b) {
            return Result.succ(1, "操作成功", null);
        } else {
            return Result.fail(400, "操作失败", "");
        }
    }

    @ApiOperation("批量删除")
    @PostMapping("/deleteUserManage")
    public Result deleteUserManage(@RequestBody List<InvStoreManage> invStoreManages) {
        List<String> ids = invStoreManages.stream().filter(item -> StringUtils.isNotBlank(item.getId())).map(BaseEntity::getId).collect(Collectors.toList());
        boolean b = invUserManageService.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.succ(400, "删除失败", "");
        }
    }

}
